//
//  LCInformationViewController.m
//  TheLCNews
//
//  Created by apple on 15/11/26.
//  Copyright © 2015年 LC. All rights reserved.
//

#import "LCInformationViewController.h"
#import "LCInfoCoCell.h"
#import "LCTitleCoCell.h"
#import "LCInfoWebVC.h"
#import "LCInfoCoheardCell.h"
#import "LCjockCoCell.h"

#import "LCInformationModel.h"
@interface LCInformationViewController ()<UICollectionViewDataSource,UICollectionViewDelegate, LCInfoCoCellDelegate, LCInfoCoheardScrollCell,LCjockCoCellDelegate>
//主页面
@property(nonatomic, strong) UICollectionView *collectionView;

@property(nonatomic, retain)UICollectionView *titleCollection;       // 标题上的CollectionView

@property(nonatomic, retain) UIButton *addChannelButton;        // 创建一个添加元素的按钮


@property(nonatomic, retain) NSMutableArray *GPSCurrentArr;     // 显示当前信息的导航滚动数据数组
@property(nonatomic, retain) NSMutableArray *GPSAllArr;         // 全部导航栏数据数组;
//@property(nonatomic, retain) GPSCollectionView *GPScollectionView; // 导航视图collectionView
@end

@implementation LCInformationViewController

-(void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = NO;
}


//上面的导航条数组

#pragma mark 初始化

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.GPSCurrentArr = [NSMutableArray arrayWithObjects:@"头条",@"NBA",@"娱乐",@"暖闻",@"电影",@"段子",@"港澳", @"历史", @"数码", @"时尚", @"游戏", @"时政", @"科技", @"军事", @"跑步", @"星座", @"评论",  @"读书", @"博报", @"酒业", @"台湾", @"国学", nil];
    }
    return self;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.view.backgroundColor = [UIColor whiteColor];
//    self.navigationController.navigationBar.translucent = NO;     // 不透明
    self.navigationController.navigationBar.hidden = YES;
//    NSArray *arr = @[@"港澳", @"历史", @"数码", @"时尚", @"游戏", @"时政", @"科技", @"军事", @"跑步", @"星座", @"评论", @"段子", @"读书", @"博报", @"酒业", @"台湾", @"国学"];
//    self.GPSAllArr = [NSMutableArray arrayWithArray:arr];
    
    
    
    // 创建导航栏collectionView
    [self mainGPScollectionView];
         //创建包含tableView的collectionView
    [self maincollectionView];      // 设置主页collectionview方法
    [self addSearchTool];           // 搜索方法
    [self addChannelTool];          // 添加频道点击方法
    
    
   }

#pragma mark  创建搜索button
- (void)addSearchTool{

}


#pragma mark 搜索button点击方法
- (void)seakAction:(UIButton *)button{

   
}



#pragma mark  创建添加频道点击方法
- (void)addChannelTool{
}

// 添加频道方法Button点击方法
- (void)addChannelAction:(UIButton *)button{
    
}

#pragma mark 创建导航collectionView
- (void)mainGPScollectionView{
   
    // 创建标题背景视图
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
    titleView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:titleView];
    
    
    
     // 创建标题滚动Collection
    UICollectionViewFlowLayout *titleFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    // 设置横向滑动,默认垂直
    titleFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    titleFlowLayout.itemSize = CGSizeMake((WIDTH - 90) / 5, 50);
    titleFlowLayout.minimumLineSpacing = 0;
    titleFlowLayout.minimumInteritemSpacing = 0;
    titleFlowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    
    self.titleCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(45, 0, WIDTH -90, 64) collectionViewLayout:titleFlowLayout];
    
    self.titleCollection.delegate = self;
    self.titleCollection.dataSource = self;
    self.titleCollection.backgroundColor = [UIColor greenColor];
    [self.titleCollection registerClass:[LCTitleCoCell class] forCellWithReuseIdentifier:@"title"];
    self.titleCollection.pagingEnabled = YES;
    [titleView addSubview:self.titleCollection];
    
}



//#pragma mark 创建包含tableView的collectionView
- (void)maincollectionView{
    // 1. 要创建CollectionView, 要先创建一个flowLayout,创建完之后才能创建collectionView
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    // 设置item的大小,item就是tableView的row
    flowLayout.itemSize = CGSizeMake(WIDTH, HEIGHT - 64 - 49);
    // 设置横向滑动,默认垂直
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    // 给items设置边框
    flowLayout.minimumInteritemSpacing = 0.;
    flowLayout.minimumLineSpacing = 0.;
    // 上\左\下\右
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    // 创建CollectionView
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT - 64 - 49) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    // 是否按页滚动
    self.collectionView.pagingEnabled = YES;
    // 是否回弹
    self.collectionView.bounces = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.collectionView];
   
   
    // 签订协议方法
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    

    
    [self.collectionView registerClass:[LCInfoCoCell class] forCellWithReuseIdentifier:@"info"];

    [self.collectionView registerClass:[LCInfoCoheardCell class] forCellWithReuseIdentifier:@"heard"];
    

    [self.collectionView registerClass:[LCjockCoCell class] forCellWithReuseIdentifier:@"jock"];
}



#pragma mark collectionView协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    // 当前导航池中有多少个导航元素就返回多少个
    NSLog(@"self.GPSCurrentArr.count ====================== %ld", self.GPSCurrentArr.count);
    return self.GPSCurrentArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    // 获取展示池中的所有数据
    NSString *str = self.GPSCurrentArr[indexPath.row];

    NSLog(@"  11111%@",str);
    if (collectionView == self.titleCollection) {
                      LCTitleCoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"title" forIndexPath:indexPath];
        cell.myLabel.text = str;
        cell.tag = 2000 + indexPath.row;
   
        return cell ;
    }
    else {
    if ([str isEqualToString:@"头条"] || [str isEqualToString:@"时尚"] || [str isEqualToString:@"游戏"] || [str isEqualToString:@"科技"] || [str isEqualToString:@"历史"] || [str isEqualToString:@"数码"] || [str isEqualToString:@"军事"] || [str isEqualToString:@"跑步"] || [str isEqualToString:@"星座"] || [str isEqualToString:@"评论"] || [str isEqualToString:@"娱乐"] || [str isEqualToString:@"读书"] || [str isEqualToString:@"博报"] || [str isEqualToString:@"酒业"]) {
        LCInfoCoheardCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"heard" forIndexPath:indexPath];
        cell.delegate = self;
        cell.scrollDelegate = self;
        cell.title = str;
        return cell;
    }
        if ([str isEqualToString:@"NBA"] || [str isEqualToString:@"电影"] || [str isEqualToString:@"时政"] || [str isEqualToString:@"国学"] || [str isEqualToString:@"港澳"] || [str isEqualToString:@"暖闻"] || [str isEqualToString:@"台湾"]) {
            LCInfoCoCell *cell1 = [collectionView dequeueReusableCellWithReuseIdentifier:@"info" forIndexPath:indexPath];
            cell1.delegate = self;
            cell1.title = str;
            return cell1;
                    }
        else if([str isEqualToString:@"段子"]){
            
            LCjockCoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"jock" forIndexPath:indexPath];
            cell.jockDelegate = self;
            cell.title = str;
            return cell;

        }
    
    
    }

        return nil;


}

#pragma mark - 滑动视图 导航栏同步
// 偏移量方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    // 滑动新闻 标题同步移动
    if (scrollView == self.collectionView) {

        if (scrollView.contentOffset.x > WIDTH && scrollView.contentOffset.x < WIDTH * (self.GPSCurrentArr.count - 2)) {
            [self.titleCollection setContentOffset:CGPointMake(1.0 * self.titleCollection.contentSize.width / self.collectionView.contentSize.width * self.collectionView.contentOffset.x - self.titleCollection.frame.size.width *1.0 / 5 * 2, 0) animated:YES];
        }
    }
    for (NSInteger i = 0; i < self.GPSCurrentArr.count; i++) {
        LCTitleCoCell *cell = (LCTitleCoCell *)[self.titleCollection viewWithTag:2000 + i];
        cell.myLabel.font = [UIFont systemFontOfSize:17];
        cell.myLabel.textColor = [UIColor blackColor];

        
    }
    NSInteger i = self.collectionView.contentOffset.x / self.collectionView.frame.size.width;
    LCTitleCoCell *cell = (LCTitleCoCell *)[self.titleCollection viewWithTag:2000 + i];
    cell.myLabel.font = [UIFont systemFontOfSize:22];
    cell.myLabel.textColor = [UIColor redColor];
}

// 标题点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.titleCollection) {
        [self.collectionView setContentOffset:CGPointMake(indexPath.row * self.view.frame.size.width, 0) animated:NO];
        for (NSInteger i = 0; i < self.GPSCurrentArr.count; i++) {
            LCTitleCoCell *cell = (LCTitleCoCell *)[self.titleCollection viewWithTag:2000 + i];
            cell.myLabel.font = [UIFont systemFontOfSize:17];
            
            cell.myLabel.textColor = [UIColor blackColor];
        }
        
        LCTitleCoCell *cell = (LCTitleCoCell *)[collectionView cellForItemAtIndexPath:indexPath];
        cell.myLabel.font = [UIFont systemFontOfSize:22];
        cell.myLabel.textColor = [UIColor redColor];

        
    
        
        
    }
}


#pragma mark -  cell跳转方法
- (void)messageWebModel:(LCInformationModel *)model{
    

    LCInfoWebVC *lcVC = [[LCInfoWebVC alloc] init];
    lcVC.dataUrl = model.commentsUrl;
    lcVC.title = model.title;
    [self.navigationController pushViewController:lcVC animated:YES];
}


- (void)skipWebView:(LCScrollModel *)model{
    LCInfoWebVC *lcVC = [[LCInfoWebVC alloc] init];
    lcVC.dataUrl = model.commentsUrl;
    lcVC.title = model.title;
    [self.navigationController pushViewController:lcVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
